package com.xbts.screen.controller.screen;

import com.xbts.common.core.constant.CacheConstants;
import com.xbts.common.core.constant.Constants;
import com.xbts.common.core.enums.ConsumeTypeEnum;
import com.xbts.common.core.enums.DateTypeEnum;
import com.xbts.common.core.enums.WorkshopTypeEnum;
import com.xbts.common.core.utils.StringUtils;
import com.xbts.common.core.web.controller.BaseController;
import com.xbts.common.core.web.domain.AjaxResult;
import com.xbts.common.core.web.domain.BaseHistogramResult;
import com.xbts.common.security.annotation.RequiresPermissions;
import com.xbts.screen.service.ISyntheticScreenServer;
import com.xbts.screen.service.IWeVirtualDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 大屏-成本
 */
@Api(tags = "大屏-成本")
@RequestMapping("/home")
@RestController
public class ScreenCostController extends BaseController {

    @Autowired
    public RedisTemplate redisTemplate;

    @Autowired
    private IWeVirtualDataService weVirtualDataService;

    @Autowired
    private ISyntheticScreenServer syntheticScreenServer;
    /**
     * 成本-总体-详情-按时间类型
     */
    @ApiOperation("成本-总体-详情-按时间类型")
    @RequiresPermissions("screen:cost:overall:detailsByDateType")
    @GetMapping("/costOverallDetailsByDateType")
    public AjaxResult costOverallDetailsByDateType(DateTypeEnum dateType){
        String showVirtualData = (String)redisTemplate.opsForValue().get(CacheConstants.SYS_SHOW_VIRTUAL_DATA);
        if (StringUtils.equals(showVirtualData, Constants.TRUE)){
            return success(weVirtualDataService.costOverallDetailsByDateType(dateType));
        } else {
            BaseHistogramResult result = syntheticScreenServer.costOverallDetailsByDateType(dateType);
            if(result == null){
                return success(weVirtualDataService.costOverallDetailsByDateType(dateType));
            }
            return success(result);
        }
    }

    /**
     * 成本-车间总体详情-按时间类型
     */
    @ApiOperation("成本-车间总体详情-按时间类型")
    @RequiresPermissions("screen:cost:workshopOverall:detailsByDateType")
    @GetMapping("/costWorkshopOverallDetailsByDateType")
    public AjaxResult costWorkshopOverallDetailsByDateType(DateTypeEnum dateType){
        String showVirtualData = (String)redisTemplate.opsForValue().get(CacheConstants.SYS_SHOW_VIRTUAL_DATA);
        if (StringUtils.equals(showVirtualData, Constants.TRUE)){
            return success(weVirtualDataService.costWorkshopOverallDetailsByDateType(dateType));
        } else {
            BaseHistogramResult result = null;
            // TODO 查询真实数据, 被阉割
            if(result == null){
                return success(weVirtualDataService.costWorkshopOverallDetailsByDateType(dateType));
            }
        }
        return success();
    }


    /**
     * 成本-分车间-详情-按时间类型-按成本类型
     */
    @ApiOperation("成本-车间总体详情-按时间类型-按成本类型")
    @RequiresPermissions("screen:cost:workshopOverall:detailsByDateTypeAndConsumeType")
    @GetMapping("/costWorkshopOverallDetailsByDateTypeAndConsumeType")
    public AjaxResult costWorkshopOverallDetailsByDateTypeAndConsumeType(DateTypeEnum dateType, ConsumeTypeEnum consumeType){
        String showVirtualData = (String)redisTemplate.opsForValue().get(CacheConstants.SYS_SHOW_VIRTUAL_DATA);
        if (StringUtils.equals(showVirtualData, Constants.TRUE)){
            return success(weVirtualDataService.costWorkshopOverallDetailsByDateTypeAndConsumeType(dateType, consumeType));
        } else {
            BaseHistogramResult result = syntheticScreenServer.costWorkshopOverallDetailsByDateTypeAndConsumeType(dateType, consumeType);
            if(result == null){
                return success(weVirtualDataService.costWorkshopOverallDetailsByDateTypeAndConsumeType(dateType, consumeType));
            }
            return success(result);
        }
    }

    /**
     * 成本-车间详情-按时间类型和车间类型
     */
    @ApiOperation("成本-车间详情-按时间类型和车间类型")
    @RequiresPermissions("screen:cost:workshop:detailsByDateType")
    @GetMapping("/costWorkshopDetailsByDateTypeAndConsumeType")
    public AjaxResult costWorkshopDetailsByDateTypeAndConsumeType(DateTypeEnum dateType, ConsumeTypeEnum consumeType, String value){
        BaseHistogramResult result = syntheticScreenServer.costWorkshopDetailsByDateTypeAndConsumeType(dateType, consumeType, value);
        return success(result);
    }

    /**
     * 成本-车间详情-按类型分组
     */
    @ApiOperation("成本-车间详情-按类型分组")
    @RequiresPermissions("screen:cost:workshop:detailsGroupByConsumeType")
    @GetMapping("/costWorkshopDetailsGroupByConsumeType")
    public AjaxResult costWorkshopDetailsGroupByConsumeType(DateTypeEnum dateType, WorkshopTypeEnum workshopType){
        // TODO 被阉割
        return success();
    }

}
